package com.xuecheng.ucenter.service;

import com.xuecheng.framework.domain.ucenter.XcCompanyUser;
import com.xuecheng.framework.domain.ucenter.XcMenu;
import com.xuecheng.framework.domain.ucenter.XcUser;
import com.xuecheng.framework.domain.ucenter.ext.XcUserExt;
import com.xuecheng.ucenter.dao.XcCompanyUserRepository;
import com.xuecheng.ucenter.dao.XcMenuMapper;
import com.xuecheng.ucenter.dao.XcUserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ShengJinFeng
 * @version V1.0
 * @ClassName UserService
 * @Description (这里用一句话描述这个类的作用)
 * @date 2020/3/11 14:36
 * @since V1.0
 */
@Service
public class UcenterService {

    @Autowired
    private XcUserRepository xcUserRepository;

    @Autowired
    private XcCompanyUserRepository xcCompanyUserRepository;

    @Autowired
    private XcMenuMapper xcMenuMapper;

    /**
     * @Description 根据账号查询用户信息
     * @author ShengJinFeng
     * @date 2020/3/11
     */
    public XcUserExt getXcUserExt(String username) {
        XcUser xcUser = findByUsername(username);
        if (xcUser == null){
            return null;
        }
        String userId = xcUser.getId();
        XcCompanyUser companyUser = xcCompanyUserRepository.findByUserId(userId);
        String companyId = companyUser.getCompanyId();
        //根据用户id查询用户得权限
        List<XcMenu> xcMenus = xcMenuMapper.selectPermissionByUserId(userId);
        //获取用户所属得公司id
        XcUserExt xcUserExt = new XcUserExt();
        BeanUtils.copyProperties(xcUser,xcUserExt);
        xcUserExt.setCompanyId(companyId);
        xcUserExt.setPermissions(xcMenus);
        return xcUserExt;
    }

    /**
     * @Description 根据用户名查询用户基本信息
     * @author ShengJinFeng
     * @date 2020/3/11
     */
    public XcUser findByUsername(String username){
        XcUser xcUser = xcUserRepository.findByUsername(username);
        return xcUser;
    }
}
